<?php
namespace jy_mjifee;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	//计划任务
	public function up_red($setting=array(),$merchid=0){
		if($setting){
			$this->setting=$setting;
		}
		//上月
		$lasttime=strtotime('-1 month');
		$month_start=strtotime(date('Ym').'01');
		$daytime=$lasttime+1000;
		$day=date('Y-m',$daytime);
		if($merchid){
			$merchs=pdo_getall('jy_merch_user',array('id'=>$merchid,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$merchs=pdo_getall('jy_merch_user',array('status'=>1,'uniacid'=>$_SESSION['uniacid']));
		}
		$merchs[]=array('id'=>0);
		foreach($merchs as $mv){
			$isadd=$this->get('feelog',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$mv['id'],'createtime >'=>$month_start),'id');
			if($isadd['id']){
				continue;
			}
			$this->merchdata=array();
			$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$mv['id']);
			$params['createtime between']=array($lasttime,$month_start);
			$this->merchdata['orderprice']=pdo_getsum('jy_mjifee_order',$params,'order_price');
			$member['merchid']=$mv['id'];
			$member['level']['type']=3;
			$this->merchdata['goodsprice']=$this->model('model')->goods_allfee($member,0,2);
			$this->merchdata['dianprice']=$this->merchdata['orderprice']-$this->merchdata['goodsprice'];
			//file_put_contents(ROOT_D.'/1.txt',ijson_encode($this->merchdata)."\n",FILE_APPEND);
			$this->up_nogoods($mv);
			$this->up_goods($mv);
		}
		return true;
	}
	public function up_nogoods($merch=array()){
			$lasttime=strtotime('-1 month');
			$month_start=strtotime(date('Ym').'01');
			$this->tui_fee=0;
			$this->bm_fee=0;
			$this->dian_fee=0;
			if($this->merchdata['dianprice']>0){
				$levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merch['id']));
				if($levels){
					foreach($levels as $lv){
						$this->upfee($lv,$day,$this->merchdata['dianprice']);
					}
				}
			}
			$log=array(
					'merchid'=>$merch['id'],
					'orderprice'=>$this->merchdata['dianprice']>0?$this->merchdata['dianprice']:0,
					'tui_fee'=>$this->tui_fee,
					'bm_fee'=>$this->bm_fee,
					'dian_fee'=>$this->dian_fee,
					'day'=>$day,
			);
			$this->add_feelog($log,0);
			return true;
	}
	public function up_goods($merch=array()){
			$lasttime=strtotime('-1 month');
			$daytime=$lasttime+1000;
			$day=date('Y-m',$daytime);
			$levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merch['id']));
			if($levels){
				$params=array('merchid'=>$merch['id'],'ignore'=>0,'uniacid'=>$_SESSION['uniacid']);
				$goodsids=pdo_getall('jy_mjifee_goods',$params);
				foreach($goodsids as $gv){
					$this->tui_fee=0;
					$this->bm_fee=0;
					$this->dian_fee=0;
					foreach($levels as $lv){
						$lv['tui_per']=$gv['tui_per'];
						$lv['bm_per']=$gv['bm_per'];
						$lv['dian_per']=$gv['dian_per'];
						$this->upfee($lv,$day,'',$gv['goods_id']);
					}
					$goodsprice=pdo_getsum('jy_mjifee_order_goods',array('ispay'=>1,'goodsid'=>$gv['goods_id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					$log=array(
						'merchid'=>$merch['id'],
						'orderprice'=>$goodsprice,
						'tui_fee'=>$this->tui_fee,
						'bm_fee'=>$this->bm_fee,
						'dian_fee'=>$this->dian_fee,
						'goodsid'=>$gv['goods_id'],
					);
					$this->add_feelog($log,1);
				}
			}
			return true;
	}
	public function add_feelog($log=array(),$type=0){
		$lasttime=strtotime('-1 month');
		$daytime=$lasttime+1000;
		$day=date('Y-m',$daytime);
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$log['merchid'],
			'goodsid'=>$log['goodsid'],
			'orderprice'=>$log['orderprice'],
			'tui_fee'=>$log['tui_fee'],
			'bm_fee'=>$log['bm_fee'],
			'dian_fee'=>$log['dian_fee'],
			'day'=>$day,
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
    public function upfee($level=array(),$day='',$dianprice=0,$goods_id=0){
		$lasttime=strtotime('-1 month');
		$month_start=strtotime(date('Ym').'01');
		$daytime=$lasttime+1000;
		$day=date('Y-m',$daytime);
		$level['tui_per']=$level['tui_per']?json_decode($level['tui_per'],true):array();
		$level['bm_per']=$level['bm_per']?json_decode($level['bm_per'],true):array();
		$level['dian_per']=$level['dian_per']?json_decode($level['dian_per'],true):array();
		paixu($level['tui_per'],'man','desc');
		paixu($level['bm_per'],'man','desc');
		paixu($level['dian_per'],'man','desc');
		$members=$this->getall('member',array('level_id'=>$level['id'],'uniacid'=>$_SESSION['uniacid']));
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$params['createtime between']=array($lasttime,$month_start);
		foreach($members as $lv){
			$lv['level']=$level;
			if(!$goods_id){
				if($lv['mid']==20609){
					//file_put_contents(ROOT_D.'/1.txt',$lv['mid'].'~~'.$this->merchdata['dianprice']."\n",FILE_APPEND);
				}
			}
			//店长
			if($level['type']==3){
				$fee=0;
				if($goods_id){
					$dianprice=$this->model('model')->goods_allfee($lv,0,2,$goods_id);
					if($lv['mid']==20609){
						//file_put_contents(ROOT_D.'/1.txt',$lv['mid'].'~~'.$dianprice."\n",FILE_APPEND);
					}
				}else{
					$dianprice=$this->merchdata['dianprice'];
				}
				foreach($level['dian_per'] as $bv){
					if($dianprice>=$bv['man']){
						$fee=$dianprice*$bv['rate']/100;
						break;
					}
				}
				$this->dian_fee +=$fee;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$lv['mid'],
					'merchid'=>$lv['merchid'],
					'money'=>$fee,
					'day'=>$day,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>4,
				);
				if($goods_id){
					$new_commission['type']=5;
					$new_commission['goods_id']=$goods_id;
				}
				$this->insert('commission',$new_commission);
			}
			//部门
			if($level['type']==2){
				$params['merchid']=$lv['merchid'];
				$params['bumenid']=$lv['bumenid'];
				if($goods_id){
					$bmprice=$this->model('model')->goods_allfee($lv,0,2,$goods_id);
				}else{
					$bmprice=pdo_getsum('jy_mjifee_order',$params,'order_price');
					$goodsprice=$this->model('model')->goods_allfee($lv,0,2);
					$bmprice -=$goodsprice;
				}
				$fee=0;
				foreach($level['bm_per'] as $bv){
					if($bmprice>=$bv['man']){
						$fee=$bmprice*$bv['rate']/100;
						break;
					}
				}
				$this->bm_fee +=$fee;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$lv['mid'],
					'merchid'=>$lv['merchid'],
					'money'=>$fee,
					'day'=>$day,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>2,
				);
				if($goods_id){
					$new_commission['type']=3;
					$new_commission['goods_id']=$goods_id;
				}
				$this->insert('commission',$new_commission);
			}
			//直推
			$fee=0;
			unset($params['bumenid']);
			$params['merchid']=$lv['merchid'];
			$params['workerid']=$lv['id'];
			if($goods_id){
				$lv['level']['type']=1;
				$tuiprice=$this->model('model')->goods_allfee($lv,0,2,$goods_id);
			}else{
				$tuiprice=pdo_getsum('jy_mjifee_order',$params,'order_price');
				unset($params['workerid']);
				$params['mid']=$lv['mid'];
				$mybuyprice=pdo_getsum('jy_mjifee_order',$params,'order_price');
				$tuiprice +=$mybuyprice;
				$lv['level']['type']=1;
				$goodsprice=$this->model('model')->goods_allfee($lv,0,2);
				$tuiprice -=$goodsprice;
			}
			foreach($level['tui_per'] as $bv){
					if($tuiprice>=$bv['man']){
						$fee=$tuiprice*$bv['rate']/100;
						break;
					}
			}
			$this->tui_fee +=$fee;
			$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$lv['merchid'],
					'mid'=>$lv['mid'],
					'money'=>$fee,
					'day'=>$day,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>0,
			);
			if($goods_id){
				$new_commission['type']=1;
				$new_commission['goods_id']=$goods_id;
			}
			$this->insert('commission',$new_commission);
		}
        return true;
    }
}